package com.daatak.application.dao;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.daatak.application.domain.ApplicationFinanceObject;
import com.daatak.application.domain.ApplicationObject;
import com.daatak.application.domain.ApplicationView;
import com.daatak.application.domain.AssetObject;

@Component
@Transactional
public class ApplicationDao implements ApplicationDaoI {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) 
	{
		
		this.sessionFactory = sessionFactory;
	}
	
	public List<ApplicationView> list()
	{
		//return hibernateTemplate.find("from ApplicationObject");
		
				Session session = sessionFactory.getCurrentSession();
				Criteria criteria = session.createCriteria(ApplicationView.class);
				return criteria.list();
	}
	
	public void create(ApplicationObject applicationObject)
	{
		
		//hibernateTemplate.saveOrUpdate(applicationObject);
		sessionFactory.getCurrentSession().merge(applicationObject);
		
		
		//TODO create a transaction manager to save the all object of application in a transaction;
	}
	public void addAsset(AssetObject assetObject)
	{
		
		sessionFactory.getCurrentSession().merge(assetObject);
		
		//TODO create a transaction manager to save the all object of application in a transaction;
	}
	
	public List<AssetObject> listAssetByAppId(Integer applicationId) {
		

		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(AssetObject.class);
		criteria.add(Restrictions.eq("referenceId", applicationId));
		return criteria.list();
	}
	//TODO:make this to return only single object.
	public ApplicationObject findById(Integer Id)
	{
		return (ApplicationObject)sessionFactory.getCurrentSession().load(ApplicationObject.class, Id);
	}
	//TODO:make this to return only single object.
		public ApplicationObject loadApplicationById(Integer Id)
		{
			//Session session = sessionFactory.getCurrentSession();
			//return (ApplicationObject)session.get(ApplicationObject.class, Id);
			return (ApplicationObject)sessionFactory.getCurrentSession().load(ApplicationObject.class,Id);
		}
	public void delete(ApplicationObject  applicationObject)
	{
		sessionFactory.getCurrentSession().delete(applicationObject);
	}
	
/*	public boolean create(Object[] object)
	{
		for (int i = 0; i < object.length; i++) {
			hibernateTemplate.saveOrUpdate(object[i]);
		}
		return true;
		
		
		//TODO create a transaction manager to save the all object of application in a transaction;
	}*/
	
	public void save(ApplicationFinanceObject financeObject)
	{
		System.out.println(financeObject);
		sessionFactory.getCurrentSession().merge(financeObject);
		
		//TODO create a transaction manager to save the all object of application in a transaction;
	}
	
	public ApplicationFinanceObject loadApplicationFinancial(Integer applicationId)
	{
		
		return (ApplicationFinanceObject)sessionFactory.getCurrentSession().get(ApplicationFinanceObject.class,applicationId);
	}

	
	public boolean create(Object[] object) {
		// TODO Auto-generated method stub
		return false;
	}
}
